﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Supermarket.Dal;

namespace Supermarket.Bll
{
    public static class CategoryManager
    {
        public static List<Category> GetAllCategories()
        {
            List<Category> categories = CategoryDB.GetAllCategories();
            return categories;
        }

        public static void Update(int categoryId, string categoryName)
        {
            if (categoryName.Length < 50)
            {
                CategoryDB.Update(categoryId, categoryName);
            }
        }

        public static void CreateCategory(string categoryName)
        {
            if (categoryName.Length < 50)
            {
                Category category = new Category();
                category.CategoryName = categoryName;
                CategoryDB.CreateCategory(category);
            }
            else
            {
                throw new ArgumentOutOfRangeException("categoryName", "Максимална дължина за име: 50 символа!");
            }            
        }

        public static bool Delete(int categoryId)
        {
            Category category = CategoryDB.GetItem(categoryId);            

            if (category != null)
            { 
                int categoryProductCount = CategoryDB.GetProductCount(category);
                if (categoryProductCount == 0)
                {
                    CategoryDB.Delete(categoryId);
                    return true;
                }
            }

            return false;
        }
    }
}
